package georah.map.layers
{

	import flash.display.Loader;
	import flash.net.URLRequest;
	
	import georah.carto.ExtentZ;
	import georah.map.MapEvent;

	/**
	 *
	 * @author yuanm
	 */
	public final class DynamicLayer extends ImageLayer
	{
		private var _cameraExtent:ExtentZ=null;

		private var _loader:Loader=createLoader();
		
		private var _provider:IDynamicLayerProvider=null;

		private var _request:URLRequest=new URLRequest();
		
		override public final function get cameraExtent():ExtentZ
        {
        	return _cameraExtent;
        }
        
        public final function set cameraExtent(value:ExtentZ):void
        {
        	_cameraExtent = value;
        }

		public final function get provider():IDynamicLayerProvider
		{
			return _provider;
		}

		public final function set provider(value:IDynamicLayerProvider):void
		{
			_provider=value;
		}

		override protected function map_changeHandler(event:MapEvent):void
		{
			super.map_changeHandler(event);

			if (event && !usePreview)
			{
				updateMaskByLoaderAsset(_loader, loadingAsset);
			}

			provider.update(map);
			_request.url=provider.layerURL;
			_loader.load(_request, loaderContext);
		}

		override protected function this_closeHandler(event:MapLayerEvent):void
		{
			cleanLoader(_loader);
			_request=null;

			super.this_closeHandler(event);
		}

		override protected function this_loadFaultHandler(event:ImageLayerEvent):void
		{
			super.this_loadFaultHandler(event);

			updateMaskByLoaderAsset(_loader, unavailableAsset);
		}

		override protected function this_loadResultHandler(event:ImageLayerEvent):void
		{
			super.this_loadResultHandler(event);

			_loader.width=width;
			_loader.height=height;
			_loader.x=-map.layers.x;
			_loader.y=-map.layers.y;

			updateContainerByLoader(_loader);
		}

	}
}
